import {Body, Controller, Get, Inject, Post, Query, ValidationError, ValidationPipe} from '@nestjs/common';
import {BaseController} from "@/modules/_/base/base.controller";
import {Supplier} from "@/entitys/supplier/supplier.entity";
import {SupplierService} from "@/modules/supplier/service/supplier.service";
import {ShopStoreSupplierService} from "@/modules/supplier/service/shopStoreSupplier.service";
import {CreateSupplierDTO} from "@/modules/supplier/dto/createSupplier.dto";
import {SupplierType} from '@/typing/supplierType';
import {classTransformerUtils} from "@/utils/classTransformerUtils";
import {validate} from "class-validator";
import {ResultUtils} from "@/utils/resultUtils";
import {CreateShopSupplierDTO} from "@/modules/supplier/dto/createShopSupplier.dto";
import {QuerySupplierDto} from "@/modules/supplier/dto/querySupplier.dto";
import {CustomValidationPipe} from "@/filter/validator/pipes/CustomValidationPipe";

@Controller("/supplier")
export class SupplierController extends BaseController<Supplier> {

    @Inject()
    private shopStoreSupplierService: ShopStoreSupplierService;

    constructor(private Service: SupplierService) {
        super(Service);
    }

    @Post("/createItem")
    async createItem(@Body() obj: any) {
        const {supplierType} = obj;

        let errors: ValidationError[];

        switch (supplierType) {
            case SupplierType.ShopStore:
                let shopSupplierObj = classTransformerUtils.plainToClass(CreateShopSupplierDTO, obj);
                errors = await validate(shopSupplierObj);
                if (errors.length > 0) {
                    return ResultUtils.error(null, errors)
                }
                return this.shopStoreSupplierService.create(classTransformerUtils.plainToClass(Supplier, shopSupplierObj));
            default:
                let supplierObj = classTransformerUtils.plainToClass(CreateSupplierDTO, obj);
                errors = await validate(supplierObj);
                if (errors.length > 0) {
                    return ResultUtils.error(null, errors)
                }
                return this.Service.create(classTransformerUtils.plainToClass(Supplier, supplierObj));
        }
    }


    @Post("/updateItem")
    async updateItem(@Body("id") id: number, @Body() obj: any) {
        const {supplierType} = obj;
        let errors: ValidationError[];
        switch (supplierType) {
            case SupplierType.ShopStore:
                let shopSupplierObj = classTransformerUtils.plainToClass(CreateShopSupplierDTO, obj);
                errors = await validate(shopSupplierObj);
                if (errors.length > 0) {
                    return ResultUtils.error(null, errors, null)
                }
                return this.shopStoreSupplierService.update(id, classTransformerUtils.plainToClass(Supplier, shopSupplierObj));
            default:
                let supplierObj = classTransformerUtils.plainToClass(CreateSupplierDTO, obj);
                errors = await validate(supplierObj);
                if (errors.length > 0) {
                    return ResultUtils.error(null, errors, null)
                }
                return this.Service.update(id, classTransformerUtils.plainToClass(Supplier, supplierObj));
        }
    }


    @Get("/options")
    async options(@Query(new CustomValidationPipe()) props: QuerySupplierDto) {
    // async options(@Query() props: any) {
        return this.Service.options(props);
    }

}
